#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int ans[20][20][20][20];
int dx[10][4]={0,0,0,1,
               0,0,1,0,
               0,1,0,0,
               1,0,0,0,
               0,0,1,1,
               0,1,1,0,
               1,1,0,0,
               0,1,1,1,
               1,1,1,0,
               1,1,1,1};
struct node
{
    int x,y,z,d;
    int cnt;
};
void bfs()
{
    queue<node> q;
    q.push({0,0,0,0,0});
    ans[0][0][0][0]=0;
    while(q.size())
    {
        node t=q.front();
        q.pop();
        int s[4]={t.x, t.y, t.z, t.d};
        int cnt=t.cnt;
        if(ans[s[0]][s[1]][s[2]][s[3]]<t.cnt) continue;
        int a[4];
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<4;j++)
            {
                a[j]=s[j]+dx[i][j];
                if(a[j]>9) a[j]=0;
            }
            if(ans[a[0]][a[1]][a[2]][a[3]]>cnt+1)
            {
                ans[a[0]][a[1]][a[2]][a[3]]=cnt+1;
                q.push({a[0],a[1],a[2],a[3],cnt+1});
            }
            for(int j=0;j<4;j++)
            {
                a[j]=s[j]-dx[i][j];
                if(a[j]<0) a[j]=9;
            }
            if(ans[a[0]][a[1]][a[2]][a[3]]>cnt+1)
            {
                ans[a[0]][a[1]][a[2]][a[3]]=cnt+1;
                q.push({a[0],a[1],a[2],a[3],cnt+1});
            }
        }
    }
}
int main()
{
    memset(ans,0x3f,sizeof(ans));
    int t;
    cin>>t;
    bfs();
//     cout<<ans[1][1][1][1];
    while(t--)
    {
        int a, b, c, d;
		int e, f, g, h;
		scanf("%1d%1d%1d%1d%1d%1d%1d%1d", &a, &b, &c, &d, &e, &f, &g, &h);
		printf("%d\n", ans[(e-a+10)%10][(f-b+10)%10][(g-c+10)%10][(h-d+10)%10]);
    }
    return 0;
}
